//
//  SFStatusCell.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/12.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SnapKit

//公共间距
let SFStatusCellMargin: CGFloat = 8
let SFStatusCellHeadImageWH: CGFloat = 35

class SFStatusCell: UITableViewCell {

    private var statusBottomViewConstraint: Constraint?


    var statusViewModel: SFStatusViewModel?{

        didSet{


            //设置cell的控件设置数据
            originView.statusViewModel = statusViewModel
            statusBottomView.statusViewModel = statusViewModel

            //先删掉bottomBar之前的约束
            self.statusBottomViewConstraint?.uninstall()

            //判断是不是有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                //如果有
                //设置数据
                retweetView.statusViewModel = statusViewModel
                //不隐藏
                retweetView.hidden = false
                //更新bottomBar的约束
                statusBottomView.snp_makeConstraints(closure: { (make) in

                    self.statusBottomViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {

                //隐藏
                retweetView.hidden = true
                //更新bottomBar的约束
                statusBottomView.snp_makeConstraints(closure: { (make) in

                    self.statusBottomViewConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()


    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //设置UI布局
    func setupUI(){

        //取消点击状态
        self.selectionStyle = .None

        //添加控件
        contentView.addSubview(originView)
        contentView.addSubview(statusBottomView)
        contentView.addSubview(retweetView)

        //设置布局

        //用户信息
        originView.snp_makeConstraints(closure: { (make) in

            make.leading.trailing.top.equalTo(contentView)

//            make.height.equalTo(200)
        })

        //转发界面
        retweetView.snp_makeConstraints { (make) in

            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originView.snp_bottom)
        }

        //toobar
        statusBottomView.snp_makeConstraints(closure: { (make) in

            make.leading.trailing.equalTo(originView)

            self.statusBottomViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint

            make.height.equalTo(35)
        })



        //设置cell的高度

        contentView.snp_makeConstraints(closure: { (make) in

            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(statusBottomView)
        })
    }


    //懒加载控件
    private lazy var originView: SFOriginView = SFOriginView()

    private lazy var statusBottomView :SFStatusBottomView = SFStatusBottomView()

    private lazy var retweetView: SFRetweetStatusView  = SFRetweetStatusView()



}
